<?php
    function ApplicationRegister($data)
    {
        $sql = "INSERT INTO c_application_register SET 
            `userId`='" . $data["userId"] . "'
            , `refCode`='" . $data["refCode"] . "'
            , `manufacturer`='" . strtoupper($data["brand"]) . "'
            , `handset`='" . strtoupper($data["model"]) . "'
            , `midp`='" . $data["midp"] . "'
            , `width`='" . $data["w"] . "'
            , `height`='" . $data["h"] . "'
            , `create_date`=" . $data["create_date"] . "
            , `day`=" . date("d") . "
            , `month`=" . date("m") . "
            , `year`=" . date("Y") . "
        ";
        $result = mysql_query($sql);
        $appRegisterId = mysql_insert_id();
        return $appRegisterId;
    }
    
    function GetGameHotApp()
    {
        $sql = "SELECT id, title, alias FROM c_game WHERE isHotApp=1 ORDER BY date_of_hot_app DESC LIMIT 1";        
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;
    }
    
    function getAdvApp($type, $box)
    {
        $sql = "SELECT id, title, picture, link, create_date, type_content, dynamic FROM c_adv WHERE status=1 AND type=" . $type . " AND box=" . $box . " ORDER BY create_date DESC LIMIT 1";        
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;    
    }  

    function GetAllAlbum($categoryId, $appRegisterId, $currentPage, $numberRecordPerPage)
    {
        $startRecord = ($currentPage - 1) * $numberRecordPerPage;
        $condition = "";
        $condition .= !empty($categoryId) ? " AND categoryId=" . $categoryId : "";
        $sql = "SELECT id, title, introtext, download, picture, create_date FROM c_album WHERE 1 " . $condition . " ORDER BY update_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
        $result = @mysql_query($sql);
        $rows = array();
        
        $list_album_id = "0";
        while($row = @mysql_fetch_assoc($result)){
            $rows[] = $row;
            $list_album_id .= "," . $row["id"];
        }                
        
        $sql = "SELECT count(id), albumId FROM c_image WHERE albumId IN (" . $list_album_id . ") GROUP BY albumId";
        $result = @mysql_query($sql);
        $replaceImage = array();                
        while($row = @mysql_fetch_assoc($result)){
            $replaceImage[$row["albumId"]] = $row["count(id)"];            
        }        
        
        $i = 0;
        while(isset($rows[$i])){
            $rows[$i]["total_image"] = isset($replaceImage[$rows[$i]["id"]]) ? $replaceImage[$rows[$i]["id"]] : 0;
            $i++;
        }
  
        if(!empty($appRegisterId)){
            $sql = "SELECT dataId FROM c_data_download WHERE dataId IN (" . $list_album_id . ") AND type=3 AND appRegisterId=" . $appRegisterId;
            $result = @mysql_query($sql);        
            $list_download_id = array();
            while($row = @mysql_fetch_assoc($result)){            
                $list_download_id[] = $row["dataId"];
            }
            
            $i = 0;
            while(isset($rows[$i])){
                if(in_array($rows[$i]["id"], $list_download_id)){
                    $rows[$i]["position"] = 1;
                } else {
                    $rows[$i]["position"] = 0;
                }
                $i++;
            }
            
            $rows = OrderArray($rows, "position", count($rows));    
        }
        return $rows;   
    }

    function GetTotalAlbum($categoryId)
    {
        $condition = "";
        $condition .= !empty($categoryId) ? " AND categoryId=" . $categoryId : "";
        $sql = "SELECT count(id) FROM c_album WHERE 1 " . $condition;
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows["count(id)"];
    }
    
    function GetAllGame($categoryId, $appRegisterId, $type, $currentPage, $numberRecordPerPage)
    {
        $startRecord = ($currentPage - 1) * $numberRecordPerPage;
        $condition = "";
        $condition .= !empty($categoryId) ? " AND categoryId=" . $categoryId : "";
        if($type==2){
            $condition .= " AND isAndroid=1";
        } else if($type==3){
            $condition .= " AND isRim=1";
        } else if($type==4){
            $condition .= " AND isIos=1";
        }
        $condition .= " AND isOnline=0 AND isSms=0";
        
        $sql = "SELECT id, title, introtext, download, picture, create_date FROM c_game WHERE 1 " . $condition . " ORDER BY update_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;        
        $result = @mysql_query($sql);
        $rows = array();
        $list_game_id = "0";
        while($row = @mysql_fetch_assoc($result)){
            $rows[] = $row;
            $list_game_id .= "," . $row["id"];
        }
        
        if(!empty($appRegisterId)){
            $sql = "SELECT dataId FROM c_data_download WHERE dataId IN (" . $list_game_id . ") AND type=1 AND appRegisterId=" . $appRegisterId;
            $result = @mysql_query($sql);        
            $list_download_id = array();
            while($row = @mysql_fetch_assoc($result)){            
                $list_download_id[] = $row["dataId"];
            }
            
            $i = 0;
            while(isset($rows[$i])){
                if(in_array($rows[$i]["id"], $list_download_id)){
                    $rows[$i]["position"] = 1;
                } else {
                    $rows[$i]["position"] = 0;
                }
                $i++;
            }
            
            $rows = OrderArray($rows, "position", count($rows));    
        }
                    
        return $rows;   
    }

    function GetTotalGame($categoryId, $type)
    {
        $condition = "";
        if($type==2){
            $condition .= " AND isAndroid=1";
        } else if($type==3){
            $condition .= " AND isRim=1";
        } else if($type==4){
            $condition .= " AND isIos=1";
        }
        $condition .= !empty($categoryId) ? " AND categoryId=" . $categoryId : "";
        $condition .= " AND isOnline=0 AND isSms=0";
        
        $sql = "SELECT count(id) FROM c_game WHERE 1 " . $condition;
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows["count(id)"];
    }
    
    function GetAllVideo($categoryId, $appRegisterId, $currentPage, $numberRecordPerPage)
    {
        $startRecord = ($currentPage - 1) * $numberRecordPerPage;
        $condition = "";
        $condition .= !empty($categoryId) ? " AND categoryId=" . $categoryId : "";
        $sql = "SELECT id, title, introtext, download, picture, create_date FROM c_video WHERE 1 " . $condition . " ORDER BY update_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
        $result = @mysql_query($sql);
        $rows = array();
        $list_video_id = "0";
        while($row = @mysql_fetch_assoc($result)){
            $rows[] = $row;
            $list_video_id .= "," . $row["id"];
        }
        
        if(!empty($appRegisterId)){
            $sql = "SELECT dataId FROM c_data_download WHERE dataId IN (" . $list_video_id . ") AND type=2 AND appRegisterId=" . $appRegisterId;
            $result = @mysql_query($sql);        
            $list_download_id = array();
            while($row = @mysql_fetch_assoc($result)){            
                $list_download_id[] = $row["dataId"];
            }
            
            $i = 0;
            while(isset($rows[$i])){
                if(in_array($rows[$i]["id"], $list_download_id)){
                    $rows[$i]["position"] = 1;
                } else {
                    $rows[$i]["position"] = 0;
                }
                $i++;
            }
            
            $rows = OrderArray($rows, "position", count($rows));    
        }
                   
        return $rows;   
    }

    function GetTotalVideo($categoryId)
    {
        $condition = "";
        $condition .= !empty($categoryId) ? " AND categoryId=" . $categoryId : "";
        $sql = "SELECT count(id) FROM c_video WHERE 1 " . $condition;
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows["count(id)"];
    }
    
    function checkVersion($type, $version)
    {
        $sql = "SELECT isNew FROM c_version WHERE `version`='" . $version . "' AND `type`='" . $type . "'";        
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;    
    }
    
    function getUser($refCode)
    {
        $sql = "SELECT id, username FROM c_user WHERE refCode='" . mysql_escape_string($refCode) . "'";
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;   
    }
    
    function cleanQuery($string)
    {        
        if(empty($string)) return $string;
        $string = mysql_escape_string(trim($string));
        
        $badWords = array(
            "/Select(.*)From/i"
            , "/Union(.*)Select/i"
            , "/Update(.*)Set/i"
            , "/Delete(.*)From/i"
            , "/Drop(.*)Table/i"
            , "/Insert(.*)Into/i"                
            , "/http/i"
            , "/--/i"
        );
        
        $string = preg_replace($badWords, "", $string);

        return $string;
    }
    
    function genUrlAdv($adv, $username)
    {
        $url = "";
        if($adv["dynamic"]==0){
            /* link tĩnh */
            $url = $adv["link"];
        } else {
            /* link động */
            if($adv["type_content"]==1){
                $url = empty($username) ? "http://taoviec.com" : "http://" . $username . "taoviec.com/mobile-game-online"; 
            } else if($adv["type_content"]==2){
                $url = empty($username) ? "http://taoviec.com" : "http://" . $username . "taoviec.com/video";
            } else if($adv["type_content"]==3){
                $url = empty($username) ? "http://taoviec.com" : "http://" . $username . "taoviec.com/album";
            } 
        } 
        return $url;
    }

    function getImage($nameImage, $folder, $date='', $type='')
    {        
        if($type!='')
            $nameImage=$type.'_'.$nameImage;
        if($date!='')
            return baseUrl() . "upload/" . $folder . "/" . date("Y", $date) . "/" . date("md", $date) . "/" . $nameImage;
        else
            return baseUrl() . "upload/" . $folder . "/" . $nameImage;
    }
    
    function genRefCode($id){
        return md5(md5($id) . "CunKIEmTieN$#$@$%");
    }
    
    function service_code(){
        return "6022";
    }
    
    function OrderArray($rows, $field, $length)
    {
        for($i=0; $i<$length-1; $i++){
            for($j=$i+1; $j<$length; $j++){
                if($rows[$i][$field] <= $rows[$j][$field]){
                    $tg = $rows[$i];
                    $rows[$i] = $rows[$j];
                    $rows[$j] = $tg;                    
                }
            }
        }

        return $rows;
    }
?>